package com.legoo.studaychineseonline.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import com.google.gson.Gson;
import com.legoo.studaychineseonline.R;
import com.legoo.studaychineseonline.activity.AuditionClassExplainActivity;
import com.legoo.studaychineseonline.activity.CustomizedActivity;
import com.legoo.studaychineseonline.activity.MessageActivity;
import com.legoo.studaychineseonline.activity.WebActivity;
import com.legoo.studaychineseonline.adapter.HomeFragmentAdapter;
import com.legoo.studaychineseonline.base.BaseFragment;
import com.legoo.studaychineseonline.bean.HomeAdBannerBean;
import com.legoo.studaychineseonline.bean.HomePicBean;
import com.legoo.studaychineseonline.bean.UpdateBean;
import com.legoo.studaychineseonline.http.API;

import com.legoo.studaychineseonline.http.XUtil;

import com.legoo.studaychineseonline.utils.ImageUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.stx.xhb.xbanner.XBanner;

import org.xutils.common.Callback;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;


public class HomeFragment extends BaseFragment implements XBanner.XBannerAdapter {
    @BindView(R.id.tv_title_left)
    TextView tvTitleLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_title_right)
    TextView tvTitleRight;
    @BindView(R.id.banner)
    XBanner banner;
    @BindView(R.id.ll_shiting)
    LinearLayout llShiting;
    @BindView(R.id.ll_yuyue)
    LinearLayout llYuYue;
    @BindView(R.id.ll_gongkai)
    LinearLayout llGongKai;
    @BindView(R.id.ll_dingzhi)
    LinearLayout llDingZhi;
    @BindView(R.id.iv_yundong)
    ImageView ivYundong;
    @BindView(R.id.sr_refresh)
    SmartRefreshLayout srRefresh;
    @BindView(R.id.rv)
    RecyclerView rv;
    Unbinder unbinder;
    private List<String> bannersList = new ArrayList<>();
    private List<HomeAdBannerBean.AdBannerBean> bannerBeen;
    private List<HomePicBean> homePicBeans = new ArrayList<>();
    private HomeFragmentAdapter homeFragmentAdapter;

    @Override
    protected void initView(View view, Bundle savedInstanceState) {
        unbinder = ButterKnife.bind(this, view);
        bannerBeen = new ArrayList<>();
        bannersList = new ArrayList<>();
        banner.setmAdapter(this);
        rv.setLayoutManager(new LinearLayoutManager(context, LinearLayout.VERTICAL, false));
        rv.setHasFixedSize(true);
        rv.setNestedScrollingEnabled(false);
        getBanner();
        srRefresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {

            }
        });
        homeFragmentAdapter = new HomeFragmentAdapter(context, homePicBeans);
        rv.setAdapter(homeFragmentAdapter);

    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_home;
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.tv_title_left, R.id.tv_title_right, R.id.ll_shiting, R.id.ll_yuyue, R.id.ll_gongkai, R.id.ll_dingzhi, R.id.iv_yundong})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_title_left:
                Intent intent = new Intent(context, WebActivity.class);
                intent.putExtra("url", API.ZAIXIANZIXIN);
                intent.putExtra("status", "0");
                intent.putExtra("title", "首页");
                startActivity(intent);
                break;
            case R.id.tv_title_right:
             startIntent(MessageActivity.class);
                break;
            case R.id.ll_shiting:
                startIntent(AuditionClassExplainActivity.class);
                break;
            case R.id.ll_yuyue:
                break;
            case R.id.ll_gongkai:
                break;
            case R.id.ll_dingzhi:
                startIntent(CustomizedActivity.class);
                break;
            case R.id.iv_yundong:
                break;
            default:
                break;
        }
    }

    @Override
    public void loadBanner(XBanner banner, View view, int position) {
        if (bannersList.size() > 0) {
            if (TextUtils.isEmpty(bannersList.get(position))) {
                ((ImageView) view).setBackgroundResource(R.mipmap.ic_launcher);
            } else {
                new ImageUtil().setImg(((ImageView) view), "https://www.aikanghuli.com" + bannersList.get(position));

            }
        }

    }

    private void getBanner() {
        bannerBeen.clear();
        bannersList.clear();
        homePicBeans.clear();
        Map<String, Object> map = new HashMap<>();
        XUtil.Post(API.ces, map, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {

                HomeAdBannerBean bean = new Gson().fromJson(result, HomeAdBannerBean.class);

                if (bean.getCode() == 1) {

                    for (int i = 0; i < bean.getList().size(); i++) {

                        bannersList.add(i, bean.getList().get(i).getImg());
                        bannerBeen.add(bean.getList().get(i));
                    }
                    homePicBeans.add(new HomePicBean("https://www.aikanghuli.com" + bannersList.get(0), API.ZAIXIANZIXIN));
                    homePicBeans.add(new HomePicBean("https://www.aikanghuli.com" + bannersList.get(0), API.ZAIXIANZIXIN));
                    homePicBeans.add(new HomePicBean("https://www.aikanghuli.com" + bannersList.get(0), API.ZAIXIANZIXIN));
                    homePicBeans.add(new HomePicBean("https://www.aikanghuli.com" + bannersList.get(0), API.ZAIXIANZIXIN));
                    homePicBeans.add(new HomePicBean("https://www.aikanghuli.com" + bannersList.get(0), API.ZAIXIANZIXIN));
                    homePicBeans.add(new HomePicBean("https://www.aikanghuli.com" + bannersList.get(0), API.ZAIXIANZIXIN));

                    homeFragmentAdapter.notifyDataSetChanged();
                    banner.setData(bannersList, null);
                    banner.setOnItemClickListener(new XBanner.OnItemClickListener() {
                        @Override
                        public void onItemClick(XBanner banner, int position) {

                        }
                    });
                }
                hidePro();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                hidePro();
                Log.e("fail", ex.getMessage());

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });


    }

}
